package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {


    @Autowired
    private StudentMapper studentMapper;


    /**
     * 违纪操作
     * @param id
     * @param score
     */
    @Override
    public void updateViolation(Integer id, Short score) {
        studentMapper.updateViolation(id,score);
    }


    /***
     * 批量删除
     * @param ids
     */
    @Override
    public void delete(List<Integer> ids) {
        studentMapper.deleteBatch(ids);
    }


    /***
     * 修改学员信息
     * @param student
     */
    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateById(student);
    }


    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @Override
    public Student getStudentInfo(Integer id) {
        return studentMapper.getStudentInfoById(id);
    }


    /***
     * 新增学员信息
     * @param student
     */
    @Override
    public void add(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setViolationCount((short) 0);
        student.setViolationScore((short) 0);
        studentMapper.insert(student);
    }

    /***
     * 分页数据查询
     * @param studentQueryParam
     * @return
     */
    @Override
    public PageResult page(StudentQueryParam studentQueryParam) {
        //分页查询
        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());

        //列表查询
        List<Student> list = studentMapper.list(studentQueryParam);

        //分页数据封装
        Page<Student> page = (Page<Student>) list;

        return new PageResult(page.getTotal(),list);
    }


}
